package com.hzhr.iot.collect.protocol.processor;

import com.hzhr.iot.collect.constant.DeviceOptions;
import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import com.hzhr.iot.collect.service.helper.Device_CollectDataServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_CollectDeviceServiceHelper;
import com.hzhr.iot.collect.service.helper.Device_IotDeviceServiceHelper;
import com.hzhr.iot.collect.util.DateUtil;
import com.hzhr.iot.collect.util.StrUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.sdk.model.collectData.CollectDataPostModel;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.model.iotDevice.IotDeviceModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 压力监测仪 自动上报
 * @author 48310
 *
 */
@Component
public class MessageProcessor53 implements MessageProcessor {
	@Autowired
	MessageUtil messageUtil;
	@Autowired
	Device_IotDeviceServiceHelper device_iotDeviceServiceHelper;
	@Autowired
	Device_CollectDeviceServiceHelper device_collectDeviceServiceHelper;
	@Autowired
	Device_CollectDataServiceHelper device_collectDataServiceHelper;
	@Autowired
	DeviceOptions deviceOptions;

	@Override
	public List<Map<MessageFieldKeyEnum, Object>> proccess(int protocolId, String deviceId, Map<MessageFieldKeyEnum, Object> parsedMessageObj) throws Exception {
		String responseCode = "0000";//响应码

		//查询物联网设备
		IotDeviceModel iotDeviceModel = device_iotDeviceServiceHelper.getByIotPlatformDeviceId(deviceId);
		Integer iotDeviceId = iotDeviceModel.getId();

		//查询采集设备
		CollectDeviceModel collectDeviceModel = device_collectDeviceServiceHelper.getByIotDeviceId(iotDeviceId);
		Integer collectDeviceId = null;
		if (collectDeviceModel != null) {
			collectDeviceId = collectDeviceModel.getId();
		}

		List currentDateList = HzhrTypeUtil.object2list(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_DATE));
		List currentTimeList = HzhrTypeUtil.object2list(parsedMessageObj.get(MessageFieldKeyEnum.CURRENT_TIME));
		List pressureList = HzhrTypeUtil.object2list(parsedMessageObj.get(MessageFieldKeyEnum.PRESSURE));
		List temperatureList = HzhrTypeUtil.object2list(parsedMessageObj.get(MessageFieldKeyEnum.TEMPERATURE));
		List cellVoltageList = HzhrTypeUtil.object2list(parsedMessageObj.get(MessageFieldKeyEnum.CELL_VOLTAGE));
		List alarmTypeList = HzhrTypeUtil.object2list(parsedMessageObj.get(MessageFieldKeyEnum.ALARM_TYPE));

		if (currentDateList != null && currentTimeList != null) {
			for (int i = 0; i < currentDateList.size(); i++) {
				String currentDate = HzhrTypeUtil.object2string(currentDateList.get(i));
				String currentTime = HzhrTypeUtil.object2string(currentTimeList.get(i));
				Date currentTm = DateUtil.strToDate(currentDate + currentTime, "yyyyMMddHHmmss");
				if (new Date().getTime() - currentTm.getTime() > 24 * 48 * 3600 * 1000L) {
					responseCode = "0115";
				}
				Double pressure = HzhrTypeUtil.object2double(pressureList.get(i)) * 0.01;
				Float temperature = HzhrTypeUtil.object2float(temperatureList.get(i));
				Float cellVoltage = HzhrTypeUtil.object2float(cellVoltageList.get(i)) * 0.1F;
				String alarmType  = HzhrTypeUtil.object2string(alarmTypeList.get(i));
				String alarmTypeDec = StrUtil.hexStr2decStr(alarmType);
				String alarmTypeBin = StrUtil.decimal2binary(Integer.parseInt(alarmTypeDec));

				int existCount = device_collectDataServiceHelper.getCountByIotDeviceIdAndCurrentTm(iotDeviceId, currentTm);

				if (existCount == 0) {
					//写入采集数据
					CollectDataPostModel collectDataPostModel = new CollectDataPostModel();
					collectDataPostModel.setIotDeviceId(iotDeviceId);
					collectDataPostModel.setCollectDeviceId(collectDeviceId);
					collectDataPostModel.setCurrentTm(currentTm.getTime());
					collectDataPostModel.setCurrentValue(pressure);
					collectDataPostModel.setPressure(pressure);
					collectDataPostModel.setTemperature(temperature);
					collectDataPostModel.setCellVoltage(cellVoltage);

					String alarmTypeCodes = "";
					int alarmTypeBinLen = alarmTypeBin.length();
					if (alarmTypeBinLen > 0 && alarmTypeBin.charAt(alarmTypeBinLen - 1) == '1') {
						alarmTypeCodes += deviceOptions.getCode("ALARM_TYPE", "QY") + ",";
					}
					if (alarmTypeBinLen > 1 && alarmTypeBin.charAt(alarmTypeBinLen - 2) == '1') {
						alarmTypeCodes += deviceOptions.getCode("ALARM_TYPE", "GY") + ",";
					}
					if (alarmTypeBinLen > 2 && alarmTypeBin.charAt(alarmTypeBinLen - 3) == '1') {
						alarmTypeCodes += deviceOptions.getCode("ALARM_TYPE", "GR") + ",";
					}
					if (alarmTypeBinLen > 3 && alarmTypeBin.charAt(alarmTypeBinLen - 4) == '1') {
						alarmTypeCodes += deviceOptions.getCode("ALARM_TYPE", "CCYC1") + ",";
					}
					if (alarmTypeBinLen > 4 && alarmTypeBin.charAt(alarmTypeBinLen - 5) == '1') {
						alarmTypeCodes += deviceOptions.getCode("ALARM_TYPE", "SZYC") + ",";
					}
					if (alarmTypeBinLen > 7 && alarmTypeBin.charAt(alarmTypeBinLen - 8) == '1') {
						alarmTypeCodes += deviceOptions.getCode("ALARM_TYPE", "DDYC1") + ",";
					}
					if (!"".equals(alarmTypeCodes)) {
						alarmTypeCodes = alarmTypeCodes.substring(0, alarmTypeCodes.length() - 1);
						collectDataPostModel.setAlarmCodes(alarmTypeCodes);
					}

					device_collectDataServiceHelper.post(collectDataPostModel);
				}
			}
		}

		//组装响应
		String controlCode = HzhrTypeUtil.object2string(parsedMessageObj.get(MessageFieldKeyEnum.CONTROL_CODE));
		String controlCodeBin = StrUtil.decimal2binary(Integer.parseInt(controlCode));
		if (controlCodeBin.charAt(4) == '1') {//需要响应时才进行响应
			Map<MessageFieldKeyEnum, Object> responseObj = messageUtil.generatePressureMonitorResponse(protocolId, parsedMessageObj, responseCode, null);
			List<Map<MessageFieldKeyEnum, Object>> result = new ArrayList<>();
			result.add(responseObj);
			return result;
		}

		return null;
	}
}
